package com.foreknow.service.impl;

import com.foreknow.bean.Users;
import com.foreknow.dao.impl.UsersDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.service.UsersService;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl  implements UsersService {
    UsersDaoImpl usersDao= new UsersDaoImpl();
    DBManager dbManager = DBManager.getInstance();
    JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
    @Override
    public int add(Users users) {
        Connection connection = dbManager.getConnection();
        jdbcTemplate.setConnection(connection);
        int a = 0;
        try {
            a = usersDao.add(users);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

        return a;
    }

    @Override
    public Users query(String adminname, String password) {
        Connection connection = dbManager.getConnection();
        jdbcTemplate.setConnection(connection);
        Users users = null;
        try {
            users = usersDao.login(adminname,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

//    public static void main(String[] args) {
//        UserServiceImpl userService = new UserServiceImpl();
//        String name = "tony";
//        String password ="123456";
//        Users users = userService.query(name,password);
//        System.out.println(users.getAdminname());
//        Users users = new Users();
//        users.setAdminname("tom");
//        users.setPassword("000000");
//        int a = userService.add(users);
//        System.out.println(a);
//    }
}
